const { ccclass, property } = cc._decorator;

@ccclass
/**主要功能：通过init方法给组件初始化并传参数，然后调用onInit方法 */
export abstract class BaseComponent<Prop = any> extends cc.Component {

    prop: Prop

    init(prop: Prop) {
        this.prop = prop
        this.onInit()
    }

    onInit(): void { }

    /**递归向父节点找组件 */
    getComponentInParent<T extends cc.Component>( comp: new () => T): T {
        let parent = this.node
        while (parent) {
            const result = parent.getComponent(comp);
            if (result) {
                return result as T;
            }
            if (parent.parent) {
                parent = parent.parent;
            } else {
                parent = null;
            }
        }
        return null;
    }
}


